package interfaces.menu.options;

import java.util.HashSet;
import java.util.List;

import org.fenggui.StandardWidget;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.event.key.Key;
import org.fenggui.event.mouse.MouseButton;
import org.fenggui.event.mouse.MousePressedEvent;
import org.fenggui.event.mouse.MouseReleasedEvent;
import org.fenggui.util.Alignment;

import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.StaticContent;
import interfaces.superWidgets.tabs.VerticalTabContent;

public class OptionTabContent extends VerticalTabContent {
	
	private StaticContent currentTabs;
	
	public OptionTabContent(int width, int height, int buttonHeight) {
		super(width, height, buttonHeight);
	}
	
	public void addMainButton(InterfaceButton button, OptionContent content) {
		super.addTabButton(button);
		final int pos = getContent().size();
		
		button.getAppearance().setAlignment(Alignment.LEFT);
		final List<InterfaceButton> tabButtons = content.getTabButtons();
		final StaticContent tabs = new StaticContent(width, buttonHeight * tabButtons.size());
		
		int y = tabs.getHeight();
		for(InterfaceButton b : tabButtons) {
			y -= buttonHeight;
			b.setSize(width, buttonHeight);
			b.setXY(buttonHeight * 2, y);
			b.getAppearance().setAlignment(Alignment.LEFT);
			tabs.addWidget(b);
		}
		
		tabs.layout();
		
		button.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(currentTabs != null) removeWidget(currentTabs);
				addWidget(tabs, pos);
				currentTabs = tabs;
				update();
				
				InterfaceButton b = tabButtons.get(0);
				MouseButton mb = MouseButton.LEFT;
				b.mousePressed(new MousePressedEvent(b, b.getX(), b.getY(), mb, 1, new HashSet<Key>()));
				b.mouseReleased(new MouseReleasedEvent(b, b.getX(), b.getY(), mb, 1, new HashSet<Key>()));
			}
		});
	}
	
	@Override
	public void addTabButton(InterfaceButton button) {}
	
	private void update() {
		int y = height;
		
		for(int i = 0; i < getContent().size(); i++) {
			StandardWidget w = (StandardWidget)getContent().get(i);
			y -= w.getHeight();
			w.setY(y);
		}
		
		layout();
	}
}